package com.hra.hrasittingposture.service;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleService;

import com.example.sittingposture.Recognition;
import com.example.sittingposture.Yolov5TFLiteDetector;
import com.hra.hrasittingposture.analysis.FullImageAnalyse;
import com.hra.hrasittingposture.utils.CameraProcess;

import java.util.ArrayList;

public class SittingPostureService extends LifecycleService {
    private static final String TAG = "SittingPostureServiceDebug";
    private Yolov5TFLiteDetector yolov5TFLiteDetector;
    private final CameraProcess cameraProcess = new CameraProcess();
    private ArrayList<Recognition> mRecognitions = null;
    private AlertDialog dialog = null;
    private boolean isShow = true;
    private final SittingPostureBinder sittingPostureBinder = new SittingPostureBinder();

    public class SittingPostureBinder extends Binder {
        public void setIsShowDialog(boolean isShowDialog) {
            isShow = isShowDialog;
        }

        public ArrayList<Recognition> getDetectResult() {
            return mRecognitions;
        }
    }

    private void start() {
        // 默认加载5n(目前只使用此模型)
        try {
            this.yolov5TFLiteDetector = new Yolov5TFLiteDetector();
            this.yolov5TFLiteDetector.initialModel(this);
            Log.i(TAG, "Success loading model" + this.yolov5TFLiteDetector.getModelFile());
        } catch (Exception e) {
            Log.e(TAG, "load model error: " + e.getMessage() + e);
        }

        FullImageAnalyse fullImageAnalyse = new FullImageAnalyse(this, yolov5TFLiteDetector, new FullImageAnalyse.Callback() {
            @Override
            public void getDetectResult(ArrayList<Recognition> recognitions) {
                mRecognitions = recognitions;
                // 遍历识别结果
                if (mRecognitions != null && !mRecognitions.isEmpty()) {
                    String result;
                    for (Recognition res : mRecognitions) {
                        result = res.getLabelName() + ", " + res.getConfidence();
                        Log.d(TAG, "recognitions=" + result);
                        showDialog(result);
                    }
                }
            }
        });
        cameraProcess.startCamera(this, fullImageAnalyse);
    }

    private void showDialog(String str) {
        if (dialog != null) {
            dialog.dismiss();
        }
        dialog = new AlertDialog.Builder(this)
                .setMessage(str)
                .create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        dialog.getWindow().setDimAmount(0);
        if (isShow && Settings.canDrawOverlays(this)) {
            dialog.show();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(@NonNull Intent intent) {
        super.onBind(intent);
        return sittingPostureBinder;
    }
}
